package com.sneaker.shower.controller;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.common.ThreadLocalCache;
import com.sneaker.shower.entity.dto.*;
import com.sneaker.shower.service.UserReceiveInfoService;
import com.sneaker.shower.service.UserService;
import com.sneaker.shower.service.WxUserService;
import com.sneaker.shower.util.JacksonUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@Slf4j
public class UserController {
    private UserService userService;
    private WxUserService wxUserService;
    private UserReceiveInfoService userReceiveInfoService;

    @Autowired
    private UserController(WxUserService wxUserService, UserService userService, UserReceiveInfoService userReceiveInfoService) {
        this.wxUserService = wxUserService;
        this.userService = userService;
        this.userReceiveInfoService = userReceiveInfoService;
    }

    @PostMapping("/user/register")
    @ApiOperation("会员注册")
    public BaseView<Boolean> register(@ApiParam("用户OpenId") String openId, @ApiParam("验证码") String verifyCode, @ApiParam("手机号") String phone) {
        return new BaseView<>(userService.register(openId, verifyCode, phone));
    }

    @GetMapping("/user/verifyCode")
    @ApiOperation(value = "发送验证码")
    public BaseView<Boolean> verifyCode(@ApiParam("手机号") String phone) {
        return new BaseView<>(userService.verifyCode(phone));
    }

    @GetMapping("/user/rolling")
    @ApiOperation(value = "充值轮训 - 前端 调用 wxpay/payInfo 下单 后轮训 接口")
    public BaseView<UserChargeRespDTO> rolling(String payGuid) {
        return new BaseView<>(userService.rolling(payGuid));
    }

    @GetMapping("/user/member/list")
    @ApiOperation(value = "所有会员信息")
    public BaseView<MemberLevelDTO> memberList() {
        return new BaseView<>(MemberLevelDTO.get());
    }

    @GetMapping("/user")
    @ApiOperation(value = "所有会员信息")
    public BaseView<UserDTO> current() {
        return new BaseView<>(ThreadLocalCache.get());
    }

    @GetMapping("/user/member/info")
    public BaseView<MemberInfoDTO> getMemberInfo() {
        return new BaseView<>(userService.getMemberInfo());
    }

    @GetMapping("/user/charge")
    @ApiOperation(value = "充值 - 返回 支付订单 guid, 前端 调用 wxpay/payInfo 下单")
    public BaseView<WxPayParamDTO> charge(Integer type) {
        return new BaseView<>(userService.charge(type));
    }

    /**
     * 用户登录  获取当前用户sessionKey 和OpenId;
     *
     * @param code 微信code
     * @return 用户信息
     */
    @GetMapping("/user/login")
    @ApiOperation(value = "用户登录，返回sessionKey以及用户OpenId", notes = "收到返回参数后，前端调用微信api，获取加密后的用户信息，" +
            "再调用'/user/info'接口，后台接口解密用户信息，返回给前端")
    public BaseView<WxMaJscode2SessionResult> login(@RequestParam("code") String code) {
        return new BaseView<>(wxUserService.login(code));
    }

    @RequestMapping(value = "/user/info", method = RequestMethod.POST, consumes = {"application/json"})
    @ApiOperation(value = "获取用户信息", notes = "调用微信api后的返回参数可直接传到该接口中，后端解密后返回当前用户信息")
    public BaseView<UserDTO> info(@RequestBody String content) {
        WxCommonReqDTO wxCommonReqDTO = JacksonUtils.toObject(WxCommonReqDTO.class, content);
        log.info("获取用户信息请求入参:{}", JacksonUtils.writeValueAsString(wxCommonReqDTO));
        return new BaseView<>(wxUserService.info(wxCommonReqDTO));
    }

    @ApiOperation(value = "修改用户备注名字")
    @PostMapping("/user/update_user_remark")
    public BaseView<Boolean> updateRemarkName(@RequestBody UserDTO userDTO) {
        return new BaseView<>(userService.updateUserRemarkName(userDTO));
    }

    @PostMapping("/user/phone")
    @ApiOperation(value = "获取当前用户绑定的手机号", notes = "和获取用户信息相同，用sessionKey和OpenId请求微信接口，返回参数传入该接口")
    public BaseView<WxMaPhoneNumberInfo> phone(@RequestBody WxCommonReqDTO wxCommonReqDTO) {
        return new BaseView<>(wxUserService.phone(wxCommonReqDTO));
    }

    @GetMapping("/user/receive_info")
    public BaseView<List<UserReceiveInfoDTO>> getReceiveInfo() {

        return new BaseView<>(userReceiveInfoService.listAll());
    }

    @GetMapping("/user/get_receive_by_id")
    public BaseView<UserReceiveInfoDTO> getReceiveById(String guid) {

        return new BaseView<>(userReceiveInfoService.getInfo(guid));
    }

    @PostMapping("/user/add_receive")
    public BaseView<Boolean> addReceive(@RequestBody UserReceiveInfoDTO userReceiveInfoDTO) {
        return new BaseView<>(userReceiveInfoService.addReceive(userReceiveInfoDTO));
    }

    @PostMapping("/user/update_receive")
    public BaseView<Boolean> updateReceive(@RequestBody UserReceiveInfoDTO userReceiveInfoDTO) {
        return new BaseView<>(userReceiveInfoService.updateReceive(userReceiveInfoDTO));
    }

    @GetMapping("/user/del_receive")
    public BaseView<Boolean> delReceive(String guid) {
        return new BaseView<>(userReceiveInfoService.delReceive(guid));
    }

    @GetMapping("/user/get_pay_type")
    @ApiOperation("获取当前用户支付可用的支付方式，需传入订单号，返回用户信息版本号 以及各个可用会员卡信息及其版本号")
    public BaseView<UserPayTypeRespDTO> getPayType(String orderGuid) {
        return new BaseView<>(userService.getUserPayType(orderGuid));
    }

    @GetMapping("/user/get_offline_pay_type")
    public BaseView<UserPayTypeRespDTO> getOfflinePayType(BigDecimal amount) {
        log.info("线下支付请求入参");
        return new BaseView<>(userService.getOfflinePayType(amount));
    }

    @GetMapping("/user/get_invited_qr_code")
    public BaseView<String> getInvitedQrCode() {

        return new BaseView<>(userService.getInvitedQrCode());
    }

    @PostMapping("/user/get_my_promotions")
    @ApiOperation("我的推广详情")
    public BaseView<TotalPromotionDTO> getMyPromotions(@RequestBody PromotionsReqDTO promotionsReqDTO) {

        return new BaseView<>(userService.getMyPromotion(promotionsReqDTO));
    }

    @PostMapping("/user/charge_status")
    @ApiOperation("充值按钮是否显示")
    public BaseView<Boolean> showCharge() {
        return new BaseView<>(userService.showCharge());
    }
}
